home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / mus / play / multiplsr.lha / progwin.c < prev    next >
C/C++ Source or Header  |  1992-09-14  |  9KB  |  474 lines

  1. /*
  2.  * MultiPlayer
  3.  * Copyright (C) 1992 Bryan Ford
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  * I (the author of MultiPlayer) can be contacted on the Internet at
  20.  * "bryan.ford@m.cc.utah.edu".  See "Player.doc" for other addresses.
  21.  *
  22.  * $Id: progwin.c,v 4.5 92/07/19 22:16:52 BAF Exp $
  23.  *
  24.  * $Log:    progwin.c,v $
  25.  * Revision 4.5  92/07/19  22:16:52  BAF
  26.  * Calling format for progload() and progsave() changed
  27.  * Fixed nasty progwin hanging bug
  28.  * 
  29.  * Revision 4.4  92/07/19  18:09:13  BAF
  30.  * Adjustment for putting localdata back into RemindNodes
  31.  * Made load/save GuidoRequester calls run off the DoList
  32.  *
  33.  * Revision 4.3  92/07/12  08:27:37  BAF
  34.  * end() waits with an EasyRequester as necessary until font requester comes down
  35.  *
  36.  * Revision 4.2  92/06/21  11:13:04  BAF
  37.  * Migrated regargs to stdargs
  38.  *
  39.  * Revision 4.1  92/06/06  19:56:14  BAF
  40.  * Major_code_cleanup
  41.  *
  42.  * Revision 3.2  92/05/25  08:24:48  BAF
  43.  * Got rid of some "please register" references I forgot about
  44.  *
  45.  * Revision 3.1  92/05/25  07:53:26  BAF
  46.  * GNU-ized.
  47.  *
  48.  *
  49.  */
  50.  
  51. // System includes
  52. #include <exec/types.h>
  53. #include <intuition/intuition.h>
  54. #include <libraries/asl.h>
  55. #include <proto/exec.h>
  56. #include <proto/dos.h>
  57. #include <proto/intuition.h>
  58. #include <proto/wb.h>
  59.  
  60. #include "bry/macros.h"
  61. #include "bry/remind.h"
  62. #include "bry/misc.h"
  63. #include "bry/guido.h"
  64.  
  65. #include "player.h"
  66.  
  67. extern GuidoSpec progwinspec, progwinfrspec;
  68. extern short progwinleft;
  69.  
  70. struct Gadget *progwinlvgad;
  71.  
  72. static long check(long sigmask);
  73. static long close(int remake);
  74. static void init(void);
  75. void progwinlistattach(void);
  76. void progwinselmod(void);
  77.  
  78. static struct MPWin mpwin = {progwinspec,&progwinleft,init,
  79.   {0},{{0},check},{{0},close}};
  80.  
  81. static struct AppWindow *awin;
  82.  
  83. static long clicksecs, clickmics;
  84.  
  85. static void *ifr;
  86. static char frsave;
  87.  
  88. static short lastsel = -1;
  89.  
  90. static struct ModNode *
  91. findnode(int num)
  92. {
  93.   struct ModNode *node = modlist.mlh_Head;
  94.  
  95.   while(1)
  96.     {
  97.       if(!node->node.mln_Succ)
  98.         return(0L);
  99.       if(num-- <= 0)
  100.         return(node);
  101.       node = node->node.mln_Succ;
  102.     }
  103. }
  104.  
  105. static void
  106. getts(int *t,int *s)
  107. {
  108.   struct ModNode *node = modlist.mlh_Head;
  109.   int num = 0;
  110.  
  111.   while(1)
  112.     {
  113.       if(!node->node.mln_Succ)
  114.         {
  115.           num = -1;
  116.           break;
  117.         }
  118.       if(node == selmod)
  119.         break;
  120.       node = node->node.mln_Succ;
  121.       num++;
  122.     }
  123.  
  124.   *s = num;
  125.   *t = *s-4;
  126.   if(*t < 0)
  127.     *t = 0;
  128. }
  129.  
  130. void
  131. progwinlistattach(void)
  132. {
  133.   int t,s;
  134.  
  135.   if(mpwin.win)
  136.     {
  137.       getts(&t,&s);
  138.       GAttachListView(mpwin.win,progwinlvgad,&modlist,t,lastsel = s);
  139.     }
  140. }
  141.  
  142.  
  143. void
  144. progwinlistdetach(void)
  145. {
  146.   if(mpwin.win)
  147.     GDetachListView(mpwin.win,progwinlvgad);
  148.   lastsel = -1;
  149. }
  150.  
  151.  
  152. void
  153. progwinselmod(void)
  154. {
  155.   int t,s;
  156.  
  157.   if(mpwin.win)
  158.     {
  159.       getts(&t,&s);
  160.       if(lastsel != s)
  161.         GSetListViewActive(mpwin.win,progwinlvgad,t,lastsel = s);
  162.     }
  163. }
  164.  
  165.  
  166. static void
  167. callback(struct GuidoMessage *im)
  168. {
  169.   if(im->Class == IDCMP_RAWKEY)
  170.     windowkey(im->Code,PROGWINCONTEXT);
  171.   else if((im->Class == IDCMP_MOUSEBUTTONS) && (im->Code == MENUDOWN))
  172.     setfinmpwin(&mpwin);
  173. }
  174.  
  175.  
  176. static long
  177. close(int remake)
  178. {
  179.   if(awin)
  180.     {
  181.       RemoveAppWindow(awin);
  182.       awin = 0L;
  183.     }
  184.   closempwin(&mpwin);
  185.  
  186.   if(remake)
  187.     showerr(openmpwin(&mpwin));
  188.  
  189.   return(0);
  190. }
  191.  
  192. static long
  193. check(long sigmask)
  194. {
  195.   if((sigmask & mpwin.sigmask) && GCheckPanel(mpwin.win,callback,0L))
  196.     setfinmpwin(&mpwin);
  197.   return(0);
  198. }
  199.  
  200.  
  201. void
  202. gui_progwinlv(struct GuidoMessage *im)
  203. {
  204.   struct ModNode *newmod;
  205.  
  206.   newmod = findnode(lastsel = im->Code);
  207.   if(newmod)
  208.     {
  209.       if((newmod == selmod) && DoubleClick(clicksecs,clickmics,im->Seconds,im->Micros))
  210.         showerr(progact(selmod));
  211.       else
  212.         GlobSetLong(&selmod,newmod,progwinselmod),
  213.         clicksecs = im->Seconds, clickmics = im->Micros;
  214.     }
  215. }
  216.  
  217.  
  218. static void attach(void) {GlobPostMod(&modlist,0);}
  219. static void detach(void) {GlobPreMod(&modlist,0);}
  220.  
  221. void
  222. gui_progwintop(void)
  223. {
  224.   if(selmod)
  225.     {
  226.       detach();
  227.       Remove(selmod);
  228.       AddHead(&modlist,selmod);
  229.       attach();
  230.     }
  231. }
  232.  
  233.  
  234. void
  235. gui_progwinup(void)
  236. {
  237.   struct ModNode *predpred;
  238.  
  239.   if((selmod) && (predpred = selmod->node.mln_Pred->mln_Pred))
  240.     {
  241.       detach();
  242.       Remove(selmod);
  243.       Insert(&modlist,selmod,predpred);
  244.       attach();
  245.     }
  246. }
  247.  
  248.  
  249. void
  250. gui_progwindown(void)
  251. {
  252.   struct ModNode *succ;
  253.  
  254.   if((selmod) && ((succ = selmod->node.mln_Succ)->node.mln_Succ))
  255.     {
  256.       detach();
  257.       Remove(selmod);
  258.       Insert(&modlist,selmod,succ);
  259.       attach();
  260.     }
  261. }
  262.  
  263.  
  264. void
  265. gui_progwinbottom(void)
  266. {
  267.   if(selmod)
  268.     {
  269.       detach();
  270.       Remove(selmod);
  271.       AddTail(&modlist,selmod);
  272.       attach();
  273.     }
  274. }
  275.  
  276.  
  277. void
  278. gui_progwinadd(void)
  279. {
  280.   showerr(reqmod(0));
  281. }
  282.  
  283.  
  284. void
  285. gui_progwindel(void)
  286. {
  287.   struct ModNode *delmod;
  288.  
  289.   if(delmod = selmod)
  290.     {
  291.       if(delmod->node.mln_Succ->mln_Succ)
  292.         GlobSetLong(&selmod,delmod->node.mln_Succ,progwinselmod);
  293.       else if(delmod->node.mln_Pred->mln_Pred)
  294.         GlobSetLong(&selmod,delmod->node.mln_Pred,progwinselmod);
  295.       progdel(delmod);
  296.     }
  297. }
  298.  
  299.  
  300. void
  301. gui_progwinclear(void)
  302. {
  303.   progclear();
  304. }
  305.  
  306.  
  307. void
  308. gui_progwinsort(void)
  309. {
  310.   extern struct MinList modlist;
  311.   struct ModNode *n2, *n3;
  312.   char flag;
  313.  
  314.   if((modlist.mlh_TailPred == (void*)&modlist) ||
  315.     (modlist.mlh_TailPred == modlist.mlh_Head))
  316.     return;
  317.   detach();
  318.   do
  319.     { // A bubble sort...how efficient!
  320.       flag = 0;
  321.       for(n2 = modlist.mlh_Head;(n3 = n2->node.mln_Succ)->node.mln_Succ;)
  322.         {
  323.           if(stricmp(n2->nameptr,n3->nameptr) > 0)
  324.             {
  325.               Remove(n2);
  326.               Insert(&modlist,n2,n3);
  327.               flag = 1;
  328.             }
  329.           else
  330.             n2 = n3;
  331.         }
  332.     }
  333.   while(flag);
  334.   attach();
  335. }
  336.  
  337.  
  338. void
  339. gui_progwinplay(void)
  340. {
  341.   if(selmod)
  342.     showerr(progact(selmod));
  343. }
  344.  
  345. void
  346. gui_progwincurrent(void)
  347. {
  348.   GlobSetLong(&selmod,curmod,0L);
  349. }
  350.  
  351. static long
  352. frcheck(void)
  353. {
  354.   struct WBArg *args;
  355.   long count;
  356.  
  357.   if(ifr && ((count = GCheckRequester(ifr,&args)) >= 0))
  358.     {
  359.     extern short progfrleft;
  360.     char *err;
  361.  
  362.       GGetRequesterLocation(ifr,&progfrleft);
  363.       if(count > 0)
  364.         {
  365.           showerr(frsave ?
  366.             ((err = progsave(args[0].wa_Lock,args[0].wa_Name)) ? err : "Program saved") :
  367.             progload(args[0].wa_Lock,args[0].wa_Name,1));
  368.         }
  369.     }
  370.   return(0);
  371. }
  372.  
  373. static struct RemindNode frchecknode = {{0},frcheck};
  374.  
  375. static long
  376. frend(void)
  377. {
  378.   if(ifr)
  379.     {
  380.       while(GDeleteRequester(ifr))
  381.         {
  382.           static struct EasyStruct easy = {
  383.             sizeof(easy),0,"MultiPlayer","Please close all file and font requesters","OK"};
  384.  
  385.           EasyRequestArgs(0,&easy,0,0);
  386.         }
  387.       ifr = 0L;
  388.     }
  389.   remind_rem(&frchecknode);
  390.   return(0);
  391. }
  392.  
  393. static struct RemindNode frendnode = {{0},frend,0,-100};
  394.  
  395. static void
  396. addfrcalls(void)
  397. {
  398.   addcalls(&frchecknode,0,0,&frendnode);
  399. }
  400.  
  401. /* Display the load/save-program requester (does not actually perform
  402.    the load or save - that comes later when the requester completes) */
  403. static int
  404. loadsave(int dummy,int save)
  405. {
  406.   struct {
  407.     struct Window *win;
  408.     char *oktext;
  409.     int saveflag;
  410.     char *hailtext;
  411.     } parms;
  412.   int rc;
  413.  
  414.   if(frsave = parms.saveflag = save)
  415.     parms.oktext = "Save", parms.hailtext = "Program to save";
  416.   else
  417.     parms.oktext = "Load", parms.hailtext = "Program to load";
  418.   parms.win = mpwin.win;
  419.   if(!ifr && !(ifr = GCreateRequester(progwinfrspec)))
  420.     showerr("File requester unavailable");
  421.   else
  422.     {
  423.       if((rc = GOpenRequester(ifr,progwinfrspec,&parms)) == 0)
  424.         showerr("File requester in use");
  425.       else if(rc < 0)
  426.         showerr("Not enough memory");
  427.       addfrcalls();
  428.     }
  429.   return(0);
  430. }
  431.  
  432. void
  433. gui_progwinload(void)
  434. {
  435.   static struct RemindNode loadnode = {{0},loadsave,0};
  436.  
  437.   adddo(&loadnode);
  438. }
  439.  
  440. void
  441. gui_progwinsave(void)
  442. {
  443.   static struct RemindNode savenode = {{0},loadsave,1};
  444.  
  445.   adddo(&savenode);
  446. }
  447.  
  448. /* Routine called by openmpwin() for the program window*/
  449. static void
  450. init(void)
  451. {
  452.   if(wbappport)
  453.     awin = AddAppWindowA(0,0,mpwin.win,wbappport,0);
  454.   progwinlistattach();
  455. }
  456.  
  457. /* Toggle the program window, bounced from bouncekludge.asm */
  458. char *
  459. progwinopenclose(void)
  460. {
  461.   if(mpwin.win)
  462.     {
  463.       setfinmpwin(&mpwin);
  464.       return(0L);
  465.     }
  466.   return(openmpwin(&mpwin));
  467. }
  468.  
  469. void
  470. gui_progwinopenclose(void)
  471. {
  472.   showerr(progwinopenclose());
  473. }
  474.